/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daos;

import daos.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entities.Fecha;
import entities.Quote;
import entities.Stock;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Juan
 */
public class QuoteJpaController implements Serializable {
    private static QuoteJpaController instance;
    
    public synchronized static QuoteJpaController getInstance(){
        if(instance == null){
            instance = new QuoteJpaController();
        }
        return instance;
    }

    public QuoteJpaController() {
        this.emf = Persistence.createEntityManagerFactory("JavaApplication9PU");
    }
    
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Quote quote) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Fecha iddate = quote.getIddate();
            if (iddate != null) {
                iddate = em.getReference(iddate.getClass(), iddate.getId());
                quote.setIddate(iddate);
            }
            Stock idstock = quote.getIdstock();
            if (idstock != null) {
                idstock = em.getReference(idstock.getClass(), idstock.getId());
                quote.setIdstock(idstock);
            }
            em.persist(quote);
            if (iddate != null) {
                iddate.getQuoteCollection().add(quote);
                iddate = em.merge(iddate);
            }
            if (idstock != null) {
                idstock.getQuoteCollection().add(quote);
                idstock = em.merge(idstock);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Quote quote) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Quote persistentQuote = em.find(Quote.class, quote.getId());
            Fecha iddateOld = persistentQuote.getIddate();
            Fecha iddateNew = quote.getIddate();
            Stock idstockOld = persistentQuote.getIdstock();
            Stock idstockNew = quote.getIdstock();
            if (iddateNew != null) {
                iddateNew = em.getReference(iddateNew.getClass(), iddateNew.getId());
                quote.setIddate(iddateNew);
            }
            if (idstockNew != null) {
                idstockNew = em.getReference(idstockNew.getClass(), idstockNew.getId());
                quote.setIdstock(idstockNew);
            }
            quote = em.merge(quote);
            if (iddateOld != null && !iddateOld.equals(iddateNew)) {
                iddateOld.getQuoteCollection().remove(quote);
                iddateOld = em.merge(iddateOld);
            }
            if (iddateNew != null && !iddateNew.equals(iddateOld)) {
                iddateNew.getQuoteCollection().add(quote);
                iddateNew = em.merge(iddateNew);
            }
            if (idstockOld != null && !idstockOld.equals(idstockNew)) {
                idstockOld.getQuoteCollection().remove(quote);
                idstockOld = em.merge(idstockOld);
            }
            if (idstockNew != null && !idstockNew.equals(idstockOld)) {
                idstockNew.getQuoteCollection().add(quote);
                idstockNew = em.merge(idstockNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = quote.getId();
                if (findQuote(id) == null) {
                    throw new NonexistentEntityException("The quote with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Quote quote;
            try {
                quote = em.getReference(Quote.class, id);
                quote.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The quote with id " + id + " no longer exists.", enfe);
            }
            Fecha iddate = quote.getIddate();
            if (iddate != null) {
                iddate.getQuoteCollection().remove(quote);
                iddate = em.merge(iddate);
            }
            Stock idstock = quote.getIdstock();
            if (idstock != null) {
                idstock.getQuoteCollection().remove(quote);
                idstock = em.merge(idstock);
            }
            em.remove(quote);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Quote> findQuoteEntities() {
        return findQuoteEntities(true, -1, -1);
    }

    public List<Quote> findQuoteEntities(int maxResults, int firstResult) {
        return findQuoteEntities(false, maxResults, firstResult);
    }

    private List<Quote> findQuoteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Quote.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Quote findQuote(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Quote.class, id);
        } finally {
            em.close();
        }
    }

    public int getQuoteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Quote> rt = cq.from(Quote.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
